/*
	Creates Figure 4 and 5 and underlying regression table for appendix.

*/

// SET DIRECTORIES
cd ""



// FIGURE 4
	use data_study2.dta, clear

	gen zero = 0
	label var zero "Control"

	label var tr_chinese "Chinese"
	label var tr_indian "Indian"
	label var tr_black "Black"
	label var tr_viet "Vietnamese"
	label var tr_control "Control"

	loc colazn = "gs0"
	loc colnon = "gs8"
	loc reglist "tr_chinese tr_indian tr_viet tr_black zero"
	
	eststo clear
	reg rate_mayor `reglist', robust
	eststo rate_mayor_a
	outreg2 using table_fig4a.xls, replace dec(3) //addtext(Covariates, Yes) ctitle() 
	

	foreach p in repasam repblack repmy repmycomm favor {
		reg mayor_`p' `reglist', robust
		eststo `p'_a
		outreg2 using table_fig4a.xls, append dec(3)
		
	}
		
	foreach v in rate_mayor favor repasam repblack repmy repmycomm { 
	
		if "`v'" != "rate_mayor" loc max = 0.2
		if "`v'" == "rate_mayor" loc max = 0.4
		
		if "`v'" == "rate_mayor" loc name "(a) Perceived Asian-ness"
		if "`v'" == "favor" loc name = "(b) Favorability" 
		if "`v'" == "repasam" loc name = "(c) Represents Asian Americans" 
		if "`v'" == "repblack" loc name = "(d) Represents Black Americans" 
		if "`v'" == "repmy" loc name = "(e) Represents me" 
		if "`v'" == "repmycomm" loc name = "(f) Represents my community" 
		
		coefplot (`v'_a, mc(`colazn') ms(c) ciopts(lw(medthick) color(`colazn'))) ///
			, drop(_cons) msize(medlarge) legend(off) ///
			omitted baselevels xline(0, lc(black)) xlabel(-.2(.1)`max') ///
			subtitle("{bf:`name'}", bcolor(white) color(black) size(small) box pos(12) ///
				bmargin(small) bexpand margin(small)) ///
			ytitle("Candidate Identity Primes", size(small)) ylabel(, labsize(small)) ///
			norecycle saving(fig_`v'.gph, replace)
	}	

	graph combine fig_rate_mayor.gph fig_favor.gph ///
		fig_repasam.gph fig_repblack.gph fig_repmy.gph fig_repmycomm.gph, rows(2)
	
	graph export Figure4.png, as(png) replace


	
	
	
// FIGURE 5	
	use data_study2.dta, clear

	label var tr_chinese "Chinese"
	label var tr_indian "Indian"
	label var tr_black "Black"
	label var tr_viet "Vietnamese"
	label var tr_control "Control"
	
		
	// Code Nationality
	// Drop if respondent national origin matches treatment 
	foreach c in chin viet indi {
		// Keep indicator for treatment for given national origin if not matching
		gen tr_treat_`c' = tr_`c' if cntry_`c' != 1
		// Drop matching national origins from control
		gen tr_control_`c' = tr_control if cntry_`c' != 1		
	}	

	// National Origin, Asianness
	eststo clear
	reg rate_mayor tr_treat_chin if inlist(treatment, "chinese", "control"), robust
		// reg rate_mayor tr_treat_chin tr_control_chin if inlist(treatment, "chinese", "control"), robust
		// To make sure regerssion is right - these two should produce same estimates on treatment.
	eststo rate_mayor_chin
	outreg2 using table_fig5a_asianness.xls, replace dec(3) ctitle("Asian-ness, Non-Chinese") //addtext(Covariates, Yes) ctitle() 
	
	reg rate_mayor tr_treat_indi if inlist(treatment, "indian", "control"), robust
	eststo rate_mayor_indi
	outreg2 using table_fig5a_asianness.xls, append dec(3) ctitle("Asian-ness, Non-Indian")
	
	reg rate_mayor tr_treat_viet if inlist(treatment, "viet", "control"), robust
	eststo rate_mayor_viet
	outreg2 using table_fig5a_asianness.xls, append dec(3) ctitle("Asian-ness, Non-Viet")
	
	foreach p in repasam repblack repmy repmycomm favor {
		
		if "`p'" == "repasam" loc fn = "table_fig5c_`p'"
		if "`p'" == "repblack" loc fn = "table_fig5d_`p'"
		if "`p'" == "repmy" loc fn = "table_fig5e_`p'"
		if "`p'" == "repmycomm" loc fn = "table_fig5f_`p'"
		if "`p'" == "favor" loc fn = "table_fig5b_`p'"
		
		// National Origin
		reg mayor_`p' tr_treat_chin if inlist(treatment, "chinese", "control"), robust
		eststo `p'_chin
		outreg2 using `fn'.xls, append dec(3) ctitle("`p', Non-Chinese")
		reg mayor_`p' tr_treat_indi if inlist(treatment, "indian", "control"), robust
		eststo `p'_indi
		outreg2 using `fn'.xls, append dec(3) ctitle("`p', Non-Indian")
		reg mayor_`p' tr_treat_viet if inlist(treatment, "viet", "control"), robust
		eststo `p'_viet
		outreg2 using `fn'.xls, append dec(3) ctitle("`p', Non-Viet")
		
		
	}
	
	// Code Region

	drop tr_treat_* tr_control_* // Recode
	// Keep indicator for treatment for given national origin if same regioin
	gen tr_treat_chin = tr_chinese if east != 1
	gen tr_treat_viet = tr_viet if southeast != 1
	gen tr_treat_indi = tr_india if south != 1
	// Drop respondents from same regional group from control
	gen tr_control_chin = tr_control if east != 1
	gen tr_control_viet = tr_control if southeast != 1
	gen tr_control_indi = tr_control if south != 1	

	// Region, Asianness
	// Note: N's should be smaller since we're dropping more respondents based on region
	reg rate_mayor tr_treat_chin if inlist(treatment, "chinese", "control"), robust
	eststo rate_mayor_east
	outreg2 using table_fig5a_asianness.xls, append dec(3) ctitle("Rate, Non-East")
	
	reg rate_mayor tr_treat_indi if inlist(treatment, "indian", "control"), robust
	eststo rate_mayor_south
	outreg2 using table_fig5a_asianness.xls, append dec(3) ctitle("Rate, Non-South")
	
	reg rate_mayor tr_treat_viet if inlist(treatment, "viet", "control"), robust
	eststo rate_mayor_southeast
	outreg2 using table_fig5a_asianness.xls, append dec(3) ctitle("Rate, Non-Southeast")
	
	foreach p in repasam repblack repmy repmycomm favor {
	
		if "`p'" == "repasam" loc fn = "table_fig6c_`p'"
		if "`p'" == "repblack" loc fn = "table_fig6d_`p'"
		if "`p'" == "repmy" loc fn = "table_fig6e_`p'"
		if "`p'" == "repmycomm" loc fn = "table_fig6f_`p'"
		if "`p'" == "favor" loc fn = "table_fig6b_`p'"
		
		// Region, Asianness
		// Note: N's should be smaller since we're dropping more respondents based on region
		reg mayor_`p' tr_treat_chin if inlist(treatment, "chinese", "control"), robust
		eststo `p'_east
		outreg2 using `fn'.xls, append dec(3) ctitle("Asian-ness, Non-East") //addtext(Covariates, Yes) ctitle() 
		reg mayor_`p' tr_treat_indi if inlist(treatment, "indian", "control"), robust
		eststo `p'_south
		outreg2 using `fn'.xls, append dec(3) ctitle("Asian-ness, Non-South")
		reg mayor_`p' tr_treat_viet if inlist(treatment, "viet", "control"), robust
		eststo `p'_southeast
		outreg2 using `fn'.xls, append dec(3) ctitle("Asian-ness, Non-Southeast")
	}
		
	label var tr_treat_chin "Chinese"
	label var tr_treat_indi "Indian"
	label var tr_treat_viet "Vietnamese"
	
	foreach v in rate_mayor favor repasam repblack repmy repmycomm { 
	
		if "`v'" != "rate_mayor" loc max = 0.2
		if "`v'" == "rate_mayor" loc max = 0.4
		
		if "`v'" == "rate_mayor" loc name "(a) Perceived Asian-ness"
		if "`v'" == "favor" loc name = "(b) Favorability" 
		if "`v'" == "repasam" loc name = "(c) Represents Asian Americans" 
		if "`v'" == "repblack" loc name = "(d) Represents Black Americans" 
		if "`v'" == "repmy" loc name = "(e) Represents me" 
		if "`v'" == "repmycomm" loc name = "(f) Represents my community" 
		
		
		coefplot (`v'_chin, label(Non-Chinese Respondents) m(circle) msize(medlarge) mc(gs0) ciopts(lw(medthick) color(gs0))) ///
			(`v'_indi, label(Non-Indian Respondents) m(diamond) msize(med) mc(gs0) ciopts(lw(medthick) color(gs0))) ///
			(`v'_viet, label(Non-Vietnamese Respondents) m(triangle) msize(med) mc(gs0) ciopts(lw(medthick) color(gs0))) ///
			(`v'_east, label(Non-East Asian Respondents) m(circle_hollow) msize(medlarge) mc(gs8) ciopts(lw(medthick) color(gs8))) ///
			(`v'_south, label(Non-South Asian Respondents) m(diamond_hollow) msize(med) mc(gs8) ciopts(lw(medthick) color(gs8))) ///
			(`v'_southeast, label(Non-Southeast Respondents) m(triangle_hollow) msize(med) mc(gs8) ciopts(lw(medthick) color(gs8))) ///
			, drop(_cons) legend(pos(12) size(vsmall) rows(2)) ///
			omitted baselevels xline(0, lc(black)) xlabel(-.2(.1)`max') ///
			subtitle("{bf:`name'}", bcolor(white) color(black) size(small) box pos(12) ///
				bmargin(small) bexpand margin(small)) ///
			ytitle("Candidate National Origin", size(small)) ylabel(, labsize(small)) ///
			norecycle saving(fig_`v'.gph, replace)
	}	

	grc1leg fig_rate_mayor.gph fig_favor.gph ///
		fig_repasam.gph fig_repblack.gph fig_repmy.gph fig_repmycomm.gph, legendfrom(fig_rate_mayor.gph) rows(2)
	graph display, xsize(6) ysize(4)
	graph export Figure5.png, as(png) replace

	
// Clean up extra files
fs *.txt *.gph
foreach f in `r(files)' {
	erase `f'
}
exit


	
